# Maintainer: Felix Yan <felixonmars@archlinux.org>

pkgname=ruby-markly
pkgver=0.10.0
_cmark_gfm_commit=766f161ef6d61019acf3a69f5099489e7d14cd49
pkgrel=1
pkgdesc='CommonMark parser and renderer. Written in C, wrapped in Ruby.'
arch=(loong64 x86_64)
url='https://github.com/ioquatix/markly'
license=(MIT)
depends=(ruby)
checkdepends=(ruby-bake ruby-bake-test ruby-bake-modernize ruby-build-files ruby-covered ruby-sus
              ruby-kramdown ruby-redcarpet)
options=(!emptydirs)
source=(https://github.com/ioquatix/markly/archive/v$pkgver/$pkgname-$pkgver.tar.gz
        https://github.com/github/cmark-gfm/archive/$_cmark_gfm_commit/cmark-gfm-$_cmark_gfm_commit.tar.gz)
sha256sums=('79546ab840da22f03faa9be0e65eeacecdf7ca569fe89599e738d6aede4abdce'
            'e95a1300cfe4c5b5ad68af6a77855df823b5d813334910a41991af0f70575d66')

prepare() {
  cd markly-$pkgver
  rmdir cmark-gfm
  ln -s ../cmark-gfm-$_cmark_gfm_commit cmark-gfm
  sed 's/~>/>=/;/signing_key/d' -i markly.gemspec
  sed '/bake-gem/d;/utopia-project/d' -i gems.rb
}

build() {
  local _gemdir="$(gem env gemdir)"
  cd markly-$pkgver
  gem build markly.gemspec
  gem install \
    --local \
    --verbose \
    --ignore-dependencies \
    --no-user-install \
    --install-dir "tmp_install/$_gemdir" \
    --bindir "tmp_install/usr/bin" \
    markly-$pkgver.gem
  find "tmp_install/$_gemdir/gems/" \
    -type f \
    \( \
        -iname "*.o" -o \
        -iname "*.c" -o \
        -iname "*.so" -o \
        -iname "*.time" -o \
        -iname "gem.build_complete" -o \
        -iname "Makefile" \
    \) \
    -delete
  rm -r tmp_install/$_gemdir/cache
}

check() {
  local _gemdir="$(gem env gemdir)"
  cd markly-$pkgver
  GEM_HOME="tmp_install/$_gemdir" sus
}

package() {
  cd markly-$pkgver
  cp -a tmp_install/* "$pkgdir"/
}
